﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MrDev.Graphics;
using MrDev.Physics;

namespace MrDev.GameObjects
{
    class GameObject
    {
        public PhysicsParameters Parameters;
        public bool IsAlive;
        public Level Level;
        public int Lives = 1;
        public List<Bullet> Bullets;
        public GameObject Alien = null;
        public bool IsVisible { get; set; }
        private bool blinkMode = false;
        private float blinkTime;
        private float blinkInterval;

        private AnimationPlayer sprite;

        public GameObject(Level level)
        {
            this.Level = level;
            IsAlive = true;
            IsVisible = true;
        }

        public bool AnimationDone
        {
            get
            {
                return sprite.AnimationDone;
            }
        }
    

        public void PlayAnimation(Animation animation)
        {
            PlayAnimation(animation, null);
            IsVisible = true;
        }

        public void StartBlinkMode(float blinkTime)
        {
            blinkMode = true;
            this.blinkTime = blinkTime;
        }

        public void StopBlinkMode()
        {
            blinkMode = false;
            IsVisible = true;
        }


        SpriteEffects? customFlip = null;
        public void PlayAnimation(Animation animation, SpriteEffects? customFlip)
        {
            this.customFlip = customFlip;
            sprite.PlayAnimation(animation);
            Parameters.SpriteSize = new Rectangle(0, 0, animation.FrameWidth, animation.FrameHeight);
        }

        public virtual void Update(GameTime gameTime)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (blinkMode)
            {
                blinkTime -= elapsedTime;
                if (blinkTime < 0)
                {
                    blinkMode = false;
                    IsVisible = false;
                }
                else
                {
                    blinkInterval -= elapsedTime;
                    if (blinkInterval < 0)
                    {
                        blinkInterval = 0.1f;
                        IsVisible = !IsVisible;
                    }
                }
            }

        }

        
        public void SetColor(Color color)
        {
            sprite.Color = color;
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (!IsVisible)
                return;
            // Flip the sprite to face the way we are moving.
            SpriteEffects flip = SpriteEffects.None;
            if (!customFlip.HasValue)
            {
                if (Parameters.Velocity.X > 0)
                    flip = SpriteEffects.FlipHorizontally;
                else if (Parameters.Velocity.X < 0)
                    flip = SpriteEffects.None;
            }
            else
                flip = customFlip.Value;
            Vector2 pixelFriendlyPos = new Vector2((int)Math.Round(Parameters.Position.X), (int)Math.Round(Parameters.Position.Y));
            sprite.Draw(gameTime, spriteBatch, pixelFriendlyPos, flip);
        }
    }
}
